﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BusinessHandler
{
    public class CategoryController : NewsDataClassesDataContext
    {
        private static CategoryController instance;
        public static CategoryController Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new CategoryController();
                }
                return instance;
            }
            set
            {
                instance = value;
            }
        }
        public List<Category> GetCategoryforInsertNews()
        {
            NewsDataClassesDataContext context = new NewsDataClassesDataContext();
            var list = (from tmp in context.Categories
                        where tmp.CategoryParentID == null && tmp.IsAddNews == true
                        select tmp).ToList();
            if (list != null)
            {
                return (List<Category>)list;
            }
            return null;
        }
        public List<Category> GetAllChildCat()
        {
            NewsDataClassesDataContext context = new NewsDataClassesDataContext();
            var list = (from tmp in context.Categories
                        where tmp.CategoryParentID != null && tmp.IsAddNews == true
                        select tmp).ToList();
            if (list != null)
            {
                return (List<Category>)list;
            }
            return null;
        }
        public List<Category> GetAllCat()
        {
            NewsDataClassesDataContext context = new NewsDataClassesDataContext();
            var list = (from tmp in context.Categories
                        select tmp).ToList();
            if (list != null)
            {
                return (List<Category>)list;
            }
            return null;
        }
        public Category GetCatByID(int id)
        {
            NewsDataClassesDataContext context = new NewsDataClassesDataContext();
            return (from tmp in context.Categories
                    where tmp.CategoryID == id
                    select tmp).SingleOrDefault();

        }
        public bool AddNewCategory(Category item)
        {
            NewsDataClassesDataContext context=new NewsDataClassesDataContext();
            try
            {
                context.Categories.InsertOnSubmit(item);
                context.SubmitChanges();
                return true;

            }
            catch
            {
                return false;
            }
        }
    }
}